layui.use(["table", "laydate", "layer"], function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layDate = layui.laydate;
    var layer = layui.layer;

    function dateModule() {
        layDate.render({
            elem: '#startDate',
            type: 'date',
            format: 'yyyyMMdd',
            trigger: 'click',
            btns: ['clear', 'now']
        });
        layDate.render({
            elem: '#endDate',
            type: 'date',
            format: 'yyyyMMdd',
            trigger: 'click',
            btns: ['clear', 'now']
        });
    }
    dateModule();

    var UpDownSmsTable = {
        tableId: "upDownSmsTable",    //表格id
        condition: {
            msisdn: "",
            iccid: "",
            imsi: "",
            startDate: "",
            endDate: ""
        }
    };

    UpDownSmsTable.initColumn = function () {
        return [[
            {field: 'msisdn', align: "center", title: 'MSISDN'},
            {field: 'iccid', align: "center", title: 'ICCID'},
            {field: 'imsi', align: "center", title: 'IMSI'},
            {field: 'startDate', align: "center", title: '开始日期'},
            {field: 'endDate', align: "center", title: '结束日期'},
            {field: 'upSuccess', align: "center", title: '上行成功(条)'},
            {field: 'upFail', align: "center", title: '上行失败(条)'},
            {field: 'downSuccess', align: "center", title: '下行成功(条)'},
            {field: 'downFail', align: "center", title: '下行失败(条)'}
        ]];
    };

    // 渲染表格
    table.render({
        elem: '#' + UpDownSmsTable.tableId,
        url: Feng.ctxPath + '/terminalCtrl/upAndDownSmsRecords',
        height: "full-98",
        cellMinWidth: 100,
        cols: UpDownSmsTable.initColumn()
    });

    /**
     * 点击查询按钮
     */
    UpDownSmsTable.search = function () {
        var msisdnVal = $("#msisdn").val();
        var iccidVal = $("#iccid").val();
        var imsiVal = $("#imsi").val();
        var startDateVal = $("#startDate").val();
        var endDateVal = $("#endDate").val();
        if ("" == msisdnVal && "" == iccidVal && "" == imsiVal) {
            Feng.error("缺少码号信息，MSISDN、ICCID、IMSI至少填写一个");
            return;
        }
        if ("" == startDateVal || "" == endDateVal) {
            Feng.error("开始日期和结束日期不能为空！");
            return;
        }
        var count = 0;
        if ("" != msisdnVal) {
            count++;
        }
        if ("" != iccidVal) {
            count++;
        }
        if ("" != imsiVal) {
            count++;
        }
        if (count > 1) {
            Feng.error("msisdn、iccid和imsi三种卡号只能任填一项！");
            return
        }

        var queryData = {};
        queryData['msisdn'] = $("#msisdn").val();
        queryData['iccid'] = $("#iccid").val();
        queryData['imsi'] = $("#imsi").val();
        queryData['startDate'] = $("#startDate").val();
        queryData['endDate'] = $("#endDate").val();
        table.reload(UpDownSmsTable.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    $("#btnSearch").click(function () {
        UpDownSmsTable.search();
    });

    /**
     * 发送下行模板短信
     */
    UpDownSmsTable.downTemplate = function () {
        layer.open({
            type: 2,
            title: '发送下行模板短信',
            area: ['550px', '530px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/terminalCtrl/toSendDownTemplateSmsPage'
        })
    };
    $("#templateSms").click(function () {
        UpDownSmsTable.downTemplate();
    });

    /**
     * 发送下行空短信
     */
    UpDownSmsTable.downBlank = function () {
        layer.open({
            type: 2,
            title: '发送下行空短信',
            area: ['500px', '250px'],
            skin: 'layui-layer-molv',
            content: Feng.ctxPath + '/terminalCtrl/toSendDownBlankSmsPage'
        })
    };
    $("#blockSms").click(function () {
        UpDownSmsTable.downBlank();
    });
});
